package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page=menuDao.findByName(queryString);
        return new PageResult(page.getTotal(),page.getResult()) ;
    }

    @Override
    public Menu findById(int id) {
        return menuDao.findById(id);
    }

    //查找parentId
    @Override
    public List<Map<String, Integer>> finParentId() {
        return menuDao.findParentId();
    }

    @Override
    public void update(Menu menu) {
        menuDao.update(menu);
    }

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public Result delete(int id) {
        long count = menuDao.isParentMenu(id);
        if(count>0){
            return new Result(false, MessageConstant.IS_PARENTMENU);
        }
        menuDao.delete(id);
        return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
    }

    public List<Menu> findAll() {
        return menuDao.findAll();
    }
}
